package ranab.gui;

import java.util.*;
import javax.swing.table.*;

import ranab.util.*;

/**
 * This table model is used to display <code>Properties</code>.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class PropertiesTableModel extends AbstractTableModel {
    
    private final static String[] COL_NAMES = {"Key", "Value"};
    
    /**
     * <code>Properties</code> object to be displayed.
     */
    protected Properties mTableProp;
    
    /**
     * All properties key.
     */
    protected Vector mTableKeys;
    
    
    /**
     * Create this <code>TableModel</code> with empty properties.
     */
    public PropertiesTableModel() {
        this(new Properties());
    } 
     
    /**
     * Create this <code>TableModel</code>.
     */
    public PropertiesTableModel(Properties prop) {
        mTableProp = prop;
        mTableKeys = new Vector();
        initializeTable();
    }
    
    /**
     * Initialize table - populate vector.
     */
    protected void initializeTable() {
        mTableKeys.clear();
        Enumeration keyNames = mTableProp.propertyNames();
        while(keyNames.hasMoreElements()) {
            String key = keyNames.nextElement().toString();
            if(key.trim().equals("")) {
                continue;
            }
            mTableKeys.add(key);
        }
        Collections.sort(mTableKeys);
    } 
    
     
    /**
     * Get column class - always string.
     */
    public Class getColumnClass(int index) {
        return String.class;
    }
     
    /**
     * Get column count.
     */
    public int getColumnCount() {
        return COL_NAMES.length;
    } 
    
    /**
     * Get column name.
     */
    public String getColumnName(int index) {
        return COL_NAMES[index];
    } 
    
    /**
     * Get row count.
     */
    public int getRowCount() {
        return mTableKeys.size();
    } 
    
    /**
     * Get value at.
     */
    public Object getValueAt(int row, int col) {
        if(col == 0) {
            return mTableKeys.get(row);
        }
        else {
            return mTableProp.getProperty(mTableKeys.get(row).toString());
        }
    } 
     
    /**
     * Is cell editable - currently true.
     */
    public boolean isCellEditable(int row, int col) {
        return true;
    } 
    
   /**
    * Set value at - does not set value - dummy metod.
    */
   public void setValueAt(Object val, int row, int col) {
   }  
   
   /**
    * Find column index.
    */
   public int findColumn(String columnName) {
        int index = -1;
        for(int i=COL_NAMES.length; --i>=0; ) {
            if (COL_NAMES[i].equals(columnName)) {
                index = i;
                break;
            }
        }
        return index;
   }
   
   /**
    * Reload properties.
    */
   public void reload() {
       initializeTable();
       fireTableDataChanged();
   }
    
   /**
    * Reload a new properties object.
    */
   public void reload(Properties prop) {
       mTableProp = prop;
       initializeTable(); 
       fireTableDataChanged();
   } 
   
   /**
    * Sort the table key column.
    */
   public void sort(boolean bAsc) {
       if(mTableKeys.size() <= 1) {
           return;
       }
       
       Comparator comp = new DefaultComparator();
       if(!bAsc) {
           comp = new ReverseComparator(comp);
       }
       Collections.sort(mTableKeys, comp);
       fireTableDataChanged();
   }
    
}    